#include "strategy_sys.h"
#pragma warning(disable : 4996)


CTrade* CStrategySys::m_pTrade = NULL;

//map<string, vector<pair<string, double> > > CStrategySys::m_mapPdt;

map<string, vector<InstInfo> > CStrategySys::m_instRead;
map<string, vector<InstInfo> > CStrategySys::m_instWrite;

bool CStrategySys::Login_TradingDay_InstRead(char* pborkeid, char* puserid, char* ppassword,  char* pTDFront, unsigned int flag)
{                                    
	return ::Login_TradingDay_InstRead(pborkeid, puserid, ppassword, pTDFront, flag, m_pTrade);
}

bool CStrategySys::Logout()
{
	return ::Logout(m_pTrade);

}


//bool CStrategySys::GetTdConnectState()
//{
//	return td_checkstate(m_pTrade);
//}

//map<string, vector<pair<string, double> > >& CStrategySys::GetMapPdt()
//{
//	//pantheios::log_NOTICE("[GetMapPdt] ", panint(&m_mapPdt));
//
//	printf("[GetMapPdt] m_mapPdt address:%x\n", &m_mapPdt);
//
//	return &m_mapPdt;
//}

bool CStrategySys::ConvertXml(char* sourceInst, char* destInst)
{
	//1. get product id and judge exist
	char pdt[5] = {0};	char* ppdt = pdt;
	char* pSource = NULL;

	for (pSource = sourceInst; !(*pSource >= '0' && *pSource <= '9'); pSource++)
	{
		*ppdt++ = *pSource;
	}

	typedef map<string, vector<InstInfo> >::iterator it_mappdt;
	it_mappdt it = m_instRead.find(pdt);


	if (it == m_instRead.end())
	{
		return false;
	}

	vector<InstInfo>& vInst = it->second;


	//string s = string(pSource);
	//string d = string("888");

	//2. if main instrument
	if (string(pSource) == string("888"))
	{
		strcpy(destInst, vInst[0].inst);

		return true;
	}




	//3. if not main
	if (string(pSource) != string("888"))
	{
		typedef vector<InstInfo>::iterator it_vInst;
		for (it_vInst vit = vInst.begin(); vit != vInst.end(); vit++)
		{
			if (string(vit->inst) == string(sourceInst))
			{
				strcpy(destInst, vit->inst);

				return true;
			}
				

		}

	}


	return false;

}


bool CStrategySys::ConvertInst(char* sourceInst, char* destInst)
{
	//1. get product id and judge exist
	char pdt[5] = {0};
	char* ppdt = pdt;

	for (char* p = sourceInst; !(*p >= '0' && *p <= '9'); p++)
	{
		*ppdt++ = *p;
	}

	typedef map<string, vector<InstInfo> >::iterator it_mappdt;
	it_mappdt it = m_instRead.find(pdt);


	if (it == m_instRead.end())
	{
		return false;
	}

	vector<InstInfo>& vInst = it->second;




	//2. convert
	
	typedef vector<InstInfo>::iterator it_vInst;
	for (it_vInst vit = vInst.begin(); vit != vInst.end(); vit++)
	{
		if (string(vit->inst) == string(sourceInst))
		{
			if (vit == vInst.begin())
			{
				string strtmp = string(pdt) + "888";


				strcpy(destInst, strtmp.c_str());
			}
			else
			{
				strcpy(destInst, vit->inst);
			}


			return true;
		}


	}

	


	return false;

}



CStrategySys::CStrategySys(string &threadid)
{
	m_threadid = threadid;

	m_reqid = 0;

	m_han=CreateEvent(NULL, false, false, NULL); 
	if (NULL == m_han)
	{
		printf("[CStrategySys] m_han NULL\n");
	}

}

CStrategySys::~CStrategySys()
{
	if (false == m_maptrademan.empty())
	{
		pantheios::log_ERROR("[~CStrategySys] m_maptrademan not NULL");
	}


}


bool CStrategySys::Sys_Init(BarInfo* pbar, char* puserdata)
{

	if (false == Sys_InitTrademan(pbar->instru))
	{
		printf("[Sys_Init] Sys_InitTrademan[%s] failed\n", pbar->instru);
		return false;
	}


	//if (false == Sys_InitInstField(pbar->instru))
	//{
	//	printf("[Sys_Init] Sys_InitInstField[%s] failed\n", pbar->instru);
	//	return false;
	//}

	if (false == Init(pbar, m_mapuserbar, puserdata))
	{
		printf("[Sys_Init] Init[%s] failed\n", pbar->instru);
		return false;
	}

	
	return true;
}


bool CStrategySys::Sys_Destroy(BarInfo* pbar)
{
	if (false == Sys_DestroyTrademan(pbar->instru))
	{
		printf("[Sys_Destroy] Sys_DestroyTrademan[%s] failed\n", pbar->instru);
		return false;
	}

	//if (false == Sys_DestroyInstField(pbar->instru))
	//{
	//	printf("[Sys_Destroy] Sys_DestroyInstField[%s] failed\n", pbar->instru);
	//	return false;
	//}


	if (false == Destroy(pbar, m_mapuserbar))
	{
		printf("[Sys_Destroy] Destroy[%s] failed\n", pbar->instru);
		return false;
	}


	return true;
}


bool CStrategySys::Sys_Edit(BarInfo* pbar, char* puserdata)
{
	if (false == Edit(pbar, puserdata))
	{
		printf("[Sys_Edit] Edit[%s] failed\n", pbar->instru);
		return false;
	}


	return true;
}

void CStrategySys::Sys_Run(BarData* ptickdata, TThostFtdcInstrumentIDType driveinst)
{
	BarSeries* pSignalbarser = NULL;


	typedef map<string, vector<BarSeries*>>::iterator it_mapuserbar;
	it_mapuserbar it = m_mapuserbar.find(driveinst);

	if (it != m_mapuserbar.end())
	{
		//set pSignalbarser
		pSignalbarser = (it->second).at(0);


		vector<BarSeries*>& vbarser = (it->second);

		typedef vector<BarSeries*>::iterator it_vbarser;
		for (it_vbarser it = vbarser.begin(); it != vbarser.end(); it++)
		{
			BarSeries* pbarser = *it;

			if (0 == strcmp(driveinst, pbarser->m_serinfo.barinfo.instru))// verify again
			{
				pbarser->Addbardata(ptickdata);
			}

		}

	}

	if (pSignalbarser)
	{
		BarData* pData = new BarData;
		if (pData)
		{
			memcpy(pData, pSignalbarser->m_pcur, sizeof(BarData));

			emit SignalSysMd(pData);
		}

		
	}


	

	Run(ptickdata, driveinst);


}




void CStrategySys::Sys_OnOrder(CThostFtdcOrderField *porder)
{
	char* orderinst = porder->InstrumentID;

	typedef map<string, OrderSysMapSeries*>::iterator it_mapser;
	it_mapser it = m_maptrademan.find(orderinst);

	if (it != m_maptrademan.end())
	{
		OrderSysMapSeries* pmapser = it->second;


		if (pmapser->FindorderAndSet(porder))
		{
			OnOrder(porder);
		}
		else
		{
			//SetConsoleColor(FOREGROUND_GREEN);  
			//printf("[Sys_OnOrder] (%d)Unmached orderref: (%s:orderref->%s)\n", m_tindex, orderinst, porder->OrderRef);//should add thread id here to diff with other strategy same subscribe inst
			//SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		}

	}
	else
	{
		/*SetConsoleColor(FOREGROUND_GREEN);  
		printf("[Sys_OnOrder] (%d)m_maptrademan instru (%s) not found\n", m_tindex, orderinst);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  */
	}

}


void CStrategySys::Sys_OnTrade(CThostFtdcTradeField *ptrade)
{
	char* tradeinst = ptrade->InstrumentID;

	typedef map<string, OrderSysMapSeries*>::iterator it_mapser;
	it_mapser it = m_maptrademan.find(tradeinst);

	if (it != m_maptrademan.end())
	{
		OrderSysMapSeries* pmapser = it->second;


		if (pmapser->Findorder_exch_sys_setvol(ptrade))
		{
			OnTrade(ptrade);
		}
		else
		{
			//SetConsoleColor(FOREGROUND_GREEN); 
			//printf("[Sys_OnTrade] (%d)Unmached trade: (%s:orderref->%s:sysid->%s:tradevol->%d)\n", m_tindex, tradeinst, ptrade->OrderRef,
			//	ptrade->OrderSysID, ptrade->Volume);//should add thread id here to diff with other strategy same subscribe inst
			//SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		}

	}
	else
	{
		//SetConsoleColor(FOREGROUND_GREEN);  
		//printf("[Sys_OnTrade] (%d)m_maptrademan instru (%s) not found\n", m_tindex, tradeinst);
		//SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
	}


}


void CStrategySys::Sys_OnCancelOrder(CThostFtdcOrderField* porder)
{
	char* orderinst = porder->InstrumentID;

	typedef map<string, OrderSysMapSeries*>::iterator it_mapser;
	it_mapser it = m_maptrademan.find(orderinst);

	if (it != m_maptrademan.end())
	{
		OrderSysMapSeries* pmapser = it->second;


		if (pmapser->Findcancelorderforsucc(porder))
		{
			OnCancelOrder(porder);
		}
		else
		{
			//SetConsoleColor(FOREGROUND_GREEN);  
			//printf("[Sys_OnCancelOrder] (%d)Unmached orderref: (%s:orderref->%s)\n", m_tindex, orderinst, porder->OrderRef);//should add thread id here to diff with other strategy same subscribe inst
			//SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		}

	}
	else
	{
		//SetConsoleColor(FOREGROUND_GREEN);  
		//printf("[Sys_OnCancelOrder] (%d)m_maptrademan instru (%s) not found\n", m_tindex, orderinst);
		//SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
	}

}

void CStrategySys::Sys_OnCancelOrderFailed(CThostFtdcInputOrderActionField* pcancelorderfailed)
{
	char* orderinst = pcancelorderfailed->InstrumentID;

	typedef map<string, OrderSysMapSeries*>::iterator it_mapser;
	it_mapser it = m_maptrademan.find(orderinst);

	if (it != m_maptrademan.end())
	{
		OrderSysMapSeries* pmapser = it->second;


		if (pmapser->Findcancelorderforfailed(pcancelorderfailed))
		{
			OnCancelOrderFailed(pcancelorderfailed);
		}
		else
		{
			//SetConsoleColor(FOREGROUND_GREEN);  
			//printf("[Sys_OnCancelOrderFailed] (%d)Unmached orderref: (%s:orderref->%s)\n", m_tindex, orderinst, pcancelorderfailed->OrderRef);//should add thread id here to diff with other strategy same subscribe inst
			//SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		}

	}
	else
	{
		//SetConsoleColor(FOREGROUND_GREEN);  
		//printf("[Sys_OnCancelOrderFailed] (%d)m_maptrademan instru (%s) not found\n", m_tindex, orderinst);
		//SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
	}


}

void CStrategySys::Sys_StrategyCom(unsigned int size, char* pdata)
{
	OnStrategyCom(size, pdata);
}


//1. set trademan m_maptrademan with commissionrate 

//2. init CStastics and  write staticsdata

void CStrategySys::Sys_OnStatistic()
{

	//--------------------1. Set CommisionRate---------------------------------------------------------
	typedef map<string, OrderSysMapSeries*>::iterator it_mapser;
	for (it_mapser it = m_maptrademan.begin(); it != m_maptrademan.end(); it++)
	{
		const string &inst = (it->first); 

		OrderSysMapSeries* pmapser = it->second;


		//qry commisionrate
		CThostFtdcInstrumentCommissionRateField* pfield = NULL;
		if (false == QrytCommissionRate((char*)inst.c_str(), pfield))
		{
			pantheios::log_ERROR("[Sys_OnStatistic] QrytCommissionRate (", inst, ") failed");
			return;
		}

		//set commisionrate
		pmapser->Setcommisionrate(pfield);

	}
	//--------------------------------------------------------------------------------------



	//--------------------2. init CStastics and Write data---------------------------------------------------------

	CStatistics* pstastis = new CStatistics(m_threadid, m_maptrademan);
	if (NULL == pstastis)
	{
		pantheios::log_ERROR("[Sys_OnStatistic] pstastis NULL");
		return;
	}
	
	CTimeCounter tc;
	tc.Start();

	pstastis->WriteData();

	SYSTEMTIME tm;
	GetLocalTime(&tm);
	//printf("[OnTimer_DB] %d:%d:%d %s elapse:%0.2f\n", tm.wHour, tm.wMinute, tm.wSecond, inst, tc.Elapse());
	log_NOTICE("[Sys_OnStatistic] ", panint(tm.wHour), ":", panint(tm.wMinute), ":", panint(tm.wSecond), " ", m_threadid, " elapse:", panreal(tc.Elapse()));

	if (pstastis)
		delete pstastis;

	//--------------------------------------------------------------------------------------

}



void CStrategySys::Sys_Getmapuserbar(map<string, vector<BarSeries*>>& mapuserbar)
{
	mapuserbar = m_mapuserbar;
}


bool CStrategySys::Sys_InitTrademan(char* inst)
{
	//char* inst = pbar->instru;

	typedef map<string, OrderSysMapSeries*>::iterator it_mapser;
	it_mapser it = m_maptrademan.find(inst);

	if (it != m_maptrademan.end())
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[Sys_InitTrademan] m_maptrademan instru (%s) exist\n", inst);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		return false;
	}


	OrderSysMapSeries* pmapser = new OrderSysMapSeries(MAX_EACHINST_MAPSER_SIZE, NULL);
	if (NULL == pmapser)
	{
		printf("[Sys_InitTrademan] pmapser[%s] Init failed\n", inst);
		return false;
	}



	pair<it_mapser , bool> ret = m_maptrademan.insert(make_pair(string(inst), pmapser));
	if (false == ret.second)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[Sys_InitTrademan] m_maptrademan instru (%s) insert failed\n", inst);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		return false;

	}

	return true;
}

bool CStrategySys::Sys_DestroyTrademan(char* inst)
{
	typedef map<string, OrderSysMapSeries*>::iterator it_mapser;
	it_mapser it = m_maptrademan.find(inst);

	if (it != m_maptrademan.end())
	{
		OrderSysMapSeries* &pmapser = it->second;

		if (pmapser)
		{
			delete pmapser;
			pmapser = NULL;
		}

		//const char* p = (it->first.c_str());

		m_maptrademan.erase(it);

	}
	else
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[Sys_DestroyTrademan] m_maptrademan instru (%s) not found\n", inst);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		return false;
	}



	return true;
}


bool CStrategySys::Sys_InitInstField(char* inst)
{
	
	typedef map<string, CThostFtdcInstrumentField*>::iterator it_map;
	it_map it = m_mapinstfield.find(inst);

	if (it != m_mapinstfield.end())
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[Sys_InitInstField] m_mapinstfield instru (%s) exist\n", inst);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		return false;
	}

	CThostFtdcInstrumentField* pfield;
	if (false == QryInstrument(inst, pfield))
	{
		printf("[Sys_InitInstField] QryInstrument (%s) failed\n", inst);
		return false;
	}


	pair<it_map , bool> ret = m_mapinstfield.insert(make_pair(string(inst), pfield));
	if (false == ret.second)
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[Sys_InitInstField] m_mapinstfield instru (%s) insert failed\n", inst);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		return false;

	}

	return true;
}


bool CStrategySys::Sys_DestroyInstField(char* inst)
{
	//char* inst = pbar->instru;

	typedef map<string, CThostFtdcInstrumentField*>::iterator it_map;
	it_map it = m_mapinstfield.find(inst);

	if (it != m_mapinstfield.end())
	{
		CThostFtdcInstrumentField* &pfield = it->second;

		if (pfield)
		{
			delete pfield;
			pfield = NULL;
		}


		m_mapinstfield.erase(it);

	}
	else
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[Sys_DestroyInstField] m_mapinstfield instru (%s) not found\n", inst);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		return false;
	}


	return true;
}



bool CStrategySys::LimitOrder(const char* inst, double price, char director, char offset, int volumn, TThostFtdcOrderRefType* porderref)
{
	OrderInput input;

	input.pricetype = THOST_FTDC_OPT_LimitPrice;	
	strcpy(input.inst, inst);
	input.price = price;
	input.director = director;
	input.offset = offset;
	input.volumn = volumn;

	/***************invoke api to trade***************************************/
	
	if (0 != m_pTrade->m_papi->ReqOrderInsert(&input, porderref))
	{
		cerr << "[LimitOrder] ReqOrderInsert failed, not write into mapser" << endl;
		return false;
	}
	/************************************************************************/
	


	/*****************Add to mapser*************************************************/

	typedef map<string, OrderSysMapSeries*>::iterator it_mapser;
	it_mapser it = m_maptrademan.find(inst);

	if (it == m_maptrademan.end())
	{
		if (false == Sys_InitTrademan((char*)inst))
		{
			return false;
		}

		it = m_maptrademan.find(inst);		
	}

	OrderSysMapSeries* pmapser = it->second;

	pmapser->Addorder(&input.inst, porderref, input.volumn, input.director, input.offset);
	/************************************************************************/



	return true;
}


bool CStrategySys::LimitOrder(const char* inst, double baseprice, unsigned int jump, char director, char offset, int volumn, TThostFtdcOrderRefType* porderref)
{

	/*****************search for PriceTick*************************************************/

	double PriceTick = 0;
	{
		typedef map<string, vector<InstInfo> >::iterator it_mappdt;
		for(it_mappdt itp = CStrategySys::m_instRead.begin();itp != CStrategySys::m_instRead.end(); ++itp)
		{
			vector<InstInfo>& vInst = itp->second;

			typedef vector<InstInfo>::iterator it_vInst;
			for (it_vInst vit = vInst.begin(); vit != vInst.end(); vit++)
			{

				if (string(vit->inst) == string(inst))
				{
					PriceTick = vit->pricetick;
				}

			}

		}

	}


	if (0 == PriceTick)
	{
		printf("[LimitOrder] _pricetick is no found\n");
		return false;
	}

	/************************************************************************/




	OrderInput input;

	input.pricetype = THOST_FTDC_OPT_LimitPrice;	
	strcpy(input.inst, inst);
	input.director = director;
	if (input.director == THOST_FTDC_D_Buy)
		input.price = baseprice + jump * PriceTick;
	else if (input.director == THOST_FTDC_D_Sell)
		input.price = baseprice - jump * PriceTick;
	else 
		input.price = baseprice + jump * PriceTick;
	input.offset = offset;
	input.volumn = volumn;


	/***************invoke api to trade***************************************/

	if (0 != m_pTrade->m_papi->ReqOrderInsert(&input, porderref))
	{
		cerr << "[LimitOrder] ReqOrderInsert failed, not write into mapser" << endl;
		return false;
	}
	/************************************************************************/



	/*****************Add to mapser*************************************************/

	typedef map<string, OrderSysMapSeries*>::iterator it_mapser;
	it_mapser it = m_maptrademan.find(inst);

	if (it == m_maptrademan.end())
	{
		if (false == Sys_InitTrademan((char*)inst))
		{
			return false;
		}

		it = m_maptrademan.find(inst);		
	}

	OrderSysMapSeries* pmapser = it->second;

	pmapser->Addorder(&input.inst, porderref, input.volumn, input.director, input.offset);
	/************************************************************************/



	return true;
}

bool CStrategySys::CancelOrder(TThostFtdcInstrumentIDType* instrument, TThostFtdcOrderRefType* orderref, 
	TThostFtdcExchangeIDType* exchangeId, TThostFtdcOrderSysIDType* ordersysId)
{

	/***************invoke api to trade***************************************/
	//only cancel current session LimitOrder
	if (0 != m_pTrade->m_papi->ReqOrderAction(instrument, orderref, exchangeId, ordersysId))
	{
		cerr << "[CancelOrder] ReqOrderAction failed" << endl;
		return false;
	}
	/************************************************************************/


	/*****************Add to mapser*************************************************/

	char* inst = (char*)instrument;

	typedef map<string, OrderSysMapSeries*>::iterator it_mapser;
	it_mapser it = m_maptrademan.find(inst);

	//NOT new pair in m_pmapmapser here
	//because if cancelOrder failed not get here
	//if successed should have new pair in m_pmapmapser in LimitOrder invoking
	if (it == m_maptrademan.end())
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[CancelOrder] m_maptrademan instru (%s) not found\n", inst);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		return false;	
	}

	OrderSysMapSeries* pmapser = it->second;

	if (false == pmapser->Addreqcanvol(orderref, exchangeId, ordersysId))
	{
		cerr << "[CancelOrder] Addreqcanvol failed" << endl;
		return false;
	}
		
	/************************************************************************/

	return true;
}


bool CStrategySys::QryInstrument(char* inst, CThostFtdcInstrumentField* &pInstrument/*out*/)
{
	//if (0 != m_pTrade->m_papi->ReqQryInvestorPosition("SR309", m_reqid))
	//{
	//	//cerr << "[QryInstrument] ReqQryInstrument %s failed" << endl;
	//	printf("[QryInstrument] ReqQryInstrument %s failed\n", inst);
	//	return false;
	//}

	//WaitForSingleObject(m_han, INFINITE);




	/***************invoke api to req***************************************/

	if (0 != m_pTrade->m_papi->ReqQryInstrument(inst, m_reqid))
	{
		//cerr << "[QryInstrument] ReqQryInstrument %s failed" << endl;
		printf("[QryInstrument] ReqQryInstrument %s failed\n", inst);
		return false;
	}
	/************************************************************************/


	WaitForSingleObject(m_han, INFINITE);

	if (m_rspid != m_reqid)
	{
		return false;
	}

	pInstrument = (CThostFtdcInstrumentField*)m_qry_pointor;

	m_qry_pointor = NULL;

	Sleep(1100);//query once per second 

	return true;
}

void CStrategySys::Sys_OnQryInstrument(CThostFtdcInstrumentField *pInstrument, int rspid)
{
	m_rspid = rspid;
	if (m_rspid == m_reqid)
	{
		//m_qry_pointor = (char*)pInstrument;

		m_qry_pointor = (char*)new CThostFtdcInstrumentField;
		if (NULL == m_qry_pointor)
			printf("[Sys_OnQryInstrument] m_qry_pointor NULL\n");
		
		memcpy(m_qry_pointor, pInstrument, sizeof(CThostFtdcInstrumentField));

		SetEvent(m_han);
	}
	else
	{
		pantheios::log_NOTICE("[Sys_OnQryInstrument] ", m_threadid, " Unmatch Qry: ", pInstrument->InstrumentID);
	}
	
}





bool CStrategySys::QryQryTradingAccount(CThostFtdcTradingAccountField* &pTradingAccount)
{

	/***************invoke api to req***************************************/

	if (0 != m_pTrade->m_papi->ReqQryTradingAccount(m_reqid))
	{
		//cerr << "[QryInstrument] ReqQryInstrument %s failed" << endl;
		printf("[QryQryTradingAccount] ReqQryTradingAccount %s failed\n");
		return false;
	}
	/************************************************************************/


	WaitForSingleObject(m_han, INFINITE);

	if (m_rspid != m_reqid)
	{
		return false;
	}

	pTradingAccount = (CThostFtdcTradingAccountField*)m_qry_pointor;

	m_qry_pointor = NULL;

	Sleep(1100);//query once per second 

	return true;
}


void CStrategySys::Sys_OnQryTradingAccount(CThostFtdcTradingAccountField *pTradingAccount, int rspid)
{
	m_rspid = rspid;
	if (m_rspid == m_reqid)
	{
		m_qry_pointor = (char*)new CThostFtdcTradingAccountField;
		if (NULL == m_qry_pointor)
			printf("[Sys_OnQryTradingAccount] m_qry_pointor NULL\n");

		memcpy(m_qry_pointor, pTradingAccount, sizeof(CThostFtdcTradingAccountField));

		SetEvent(m_han);
	}
	else
	{
		pantheios::log_NOTICE("[Sys_OnQryTradingAccount] ", m_threadid, " Unmatch Qry");
	}

}


bool CStrategySys::QrytCommissionRate(char* inst, CThostFtdcInstrumentCommissionRateField* &pInstrumentCommissionRate/*out*/)
{

	/***************invoke api to req***************************************/

	if (0 != m_pTrade->m_papi->ReqQryInstrumentCommissionRate(inst, m_reqid))
	{
		pantheios::log_ERROR("[QrytCommissionRate] ReqQryInstrumentCommissionRate (", inst, ") failed");
		return false;
	}
	/************************************************************************/


	WaitForSingleObject(m_han, INFINITE);

	if (m_rspid != m_reqid)
	{
		return false;
	}

	pInstrumentCommissionRate = (CThostFtdcInstrumentCommissionRateField*)m_qry_pointor;

	m_qry_pointor = NULL;

	Sleep(1100);//query once per second 

	return true;
}


void CStrategySys::Sys_OnQrytCommissionRate(CThostFtdcInstrumentCommissionRateField *pInstrumentCommissionRate, int rspid)
{
	m_rspid = rspid;
	if (m_rspid == m_reqid)
	{
		m_qry_pointor = (char*)new CThostFtdcInstrumentCommissionRateField;
		if (NULL == m_qry_pointor)
			printf("[Sys_OnQrytCommissionRate] m_qry_pointor NULL\n");

		memcpy(m_qry_pointor, pInstrumentCommissionRate, sizeof(CThostFtdcInstrumentCommissionRateField));

		SetEvent(m_han);
	}
	else
	{
		pantheios::log_NOTICE("[Sys_OnQrytCommissionRate] ", m_threadid, " Unmatch Qry");
	}

}

bool CStrategySys::Getmap(char* inst, TThostFtdcOrderRefType* porder, OrderSysMap* &poutmap)
{
	typedef map<string, OrderSysMapSeries*>::iterator it_mapser;
	it_mapser it = m_maptrademan.find(inst);

	if (it == m_maptrademan.end())
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[Getmap] m_maptrademan instru (%s) not found\n", inst);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		return false;	
	}

	OrderSysMapSeries* pmapser = it->second;

	return pmapser->Getmap(porder, poutmap);
}




bool CStrategySys::Getmap_New(char* inst, TThostFtdcOrderRefType orderref, OrderSysMap* &poutmap)
{
	typedef map<string, OrderSysMapSeries*>::iterator it_mapser;
	it_mapser it = m_maptrademan.find(inst);

	if (it == m_maptrademan.end())
	{
		SetConsoleColor(FOREGROUND_GREEN);  
		printf("[Getmap_New] m_maptrademan instru (%s) not found\n", inst);
		SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);  
		return false;	
	}

	OrderSysMapSeries* pmapser = it->second;

	return pmapser->Getmap_New(orderref, poutmap);
}















